package com.example.lanouhn.firstapp.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.AddressManagementAdapter;
import com.example.lanouhn.firstapp.activity.application.FirstApplication;
import com.example.lanouhn.firstapp.activity.model.AddressManagement;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

//地址管理页面
public class AddressManagementActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private AddressManagementAdapter adapter;
    private List<AddressManagement> list;

    //关于我们--跳转
    private TextView tv_aboutOur;
    //免责声明
    private TextView tv_disclaimer;
    //商务合作
    private TextView tv_cooperate;
    //帮助中心
    private TextView tv_help;
    //添加新地址
    private TextView tv_addAddress;
    //注销
    private TextView tv_cancellation;
    //返回
    private ImageView iv_back;
    //个人中心
    private TextView mineCenter;
    //删除地址
    private TextView address_delete;
    //暂无记录
    private TextView tv_noRecord;

    private DbManager dbManager;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);


        //获取上个页面输入框的内容
        String name = intent.getStringExtra("name");
        String phone = intent.getStringExtra("phone");
        String province = intent.getStringExtra("province");
        String country = intent.getStringExtra("country");
        String street = intent.getStringExtra("street");

        //设置数据库数据
        AddressManagement address = new AddressManagement();
        address.setName(name);
        address.setPhone(phone);
        address.setProvince(province);
        address.setCountry(country);
        address.setStreet(street);

        //数据存入数据库

        dbManager = x.
                getDb(((FirstApplication) (getApplicationContext()))
                        .getDaoConfig());
        try {
            dbManager.save(address);
            list = dbManager.selector(AddressManagement.class).findAll();

            for (int i = 0; i < list.size(); i++) {
                Log.i("aaa", " id = " + list.get(i).getId() +
                        " name = " + list.get(i).getName() +
                        " phone = " + list.get(i).getPhone() +
                        " province = " + list.get(i).getProvince() +
                        " country = " + list.get(i).getCountry() +
                        " street = " + list.get(i).getStreet());

            }
            tv_noRecord.setVisibility(View.GONE);
            adapter = new AddressManagementAdapter(list, AddressManagementActivity.this);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_management);


        //找到控件
        initView();
        //监听事件
        initEvent();




            /*
        //删除地址

        try {
            //dbManager.delete(list);
            list = dbManager.findAll(AddressManagement.class);
            adapter = new AddressManagementAdapter(list, AddressManagementActivity.this);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }

*/
    }

    private void initEvent() {

        //关于我们--的点击事件
        tv_aboutOur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressManagementActivity.this, AboutOurActivity.class);
                startActivity(intent);
            }
        });

        //免责声明--点击事件
        tv_disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressManagementActivity.this, DisclaimerActivity.class);
                startActivity(intent);
            }
        });
        //商务合作--点击事件
        tv_cooperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressManagementActivity.this, CooperateActivity.class);
                startActivity(intent);
            }
        });
        //帮助中心--点击事件
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressManagementActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });
        //注销--点击事件
        tv_cancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressManagementActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        //添加新地址--点击事件
        tv_addAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddressManagementActivity.this, AddAddressActivity.class);
                startActivityForResult(intent,101);
            }
        });
        //个人中心--点击事件
        mineCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();
            }
        });
    }

    //找到控件
    private void initView() {

        tv_noRecord = (TextView) findViewById(R.id.tv_noRecord);
        address_delete = (TextView) findViewById(R.id.address_delete);

        mineCenter = (TextView) findViewById(R.id.mineCenter);
        iv_back = (ImageView) findViewById(R.id.iv_back);

        tv_addAddress = (TextView) findViewById(R.id.tv_addAddress);
        //绑定控件
        tv_aboutOur = (TextView) findViewById(R.id.mine_aboutOur);
        tv_disclaimer = (TextView) findViewById(R.id.mine_disclaimer);
        tv_cooperate = (TextView) findViewById(R.id.mine_cooperate);
        tv_help = (TextView) findViewById(R.id.mine_help);
        tv_cancellation = (TextView) findViewById(R.id.mine_cancellation);

        recyclerView = (RecyclerView) findViewById(R.id.address_recyclerView);
        GridLayoutManager glm = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(glm);

    }


}